#include "DX11LightingManager.h"
#include "ObjMeshManager.h"
#include "DX11ErrorReport.h"

DX11LightingManager::DX11LightingManager()
{
	// set default light
	Light* l = new Light;
	l->ambient = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
	l->colour = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
	l->direction = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f);
	m_lights["DEFAULT"] = *l;
	delete l;

	TheObjMeshManager::Instance()->LoadObj("../../Assets/lightbulb.obj", "lightbulb", L"Tutorial02.fx");
}

void DX11LightingManager::SetLight(const std::string& lightTag, const Light& light)
{
	m_lights[lightTag] = light;
}

const Light& DX11LightingManager::GetLight(const std::string& lightTag)
{
	CheckLight(lightTag);
	return m_lights[lightTag];
}

void DX11LightingManager::Draw(const std::string& lightTag)
{
	CheckLight(lightTag);

	XMFLOAT4 lightPos = m_lights[lightTag].direction;
	XMFLOAT4 lightAmb = m_lights[lightTag].ambient;
	m_lights[lightTag].ambient = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);

	TheObjMeshManager::Instance()->Translate("lightbulb", lightPos.x, lightPos.y, lightPos.z);
	TheObjMeshManager::Instance()->Draw("lightbulb");

	m_lights[lightTag].ambient = lightAmb;
}

void DX11LightingManager::DrawAll()
{
	for (std::map<std::string, Light>::iterator it = m_lights.begin(); it != m_lights.end(); ++it)
	{
		XMFLOAT4 lightPos = it->second.direction;
		XMFLOAT4 lightAmb = it->second.ambient;
		it->second.ambient = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);

		TheObjMeshManager::Instance()->Translate("lightbulb", lightPos.x, lightPos.y, lightPos.z);
		TheObjMeshManager::Instance()->Draw("lightbulb");

		it->second.ambient = lightAmb;
	}
}

const void DX11LightingManager::CheckLight(const std::string& lightTag)
{
	std::map<std::string, Light>::iterator it = m_lights.find(lightTag);

	if(it == m_lights.end())
	{
		std::wstring errorMsg;
		errorMsg.assign(lightTag.begin(), lightTag.end());
		errorMsg = L"No such light: " + errorMsg;
		DX11ErrorMsg(errorMsg.c_str(), L"ObjMeshManager", MB_YESNO);
	}
}